package com.y.architecture.speech;

import android.content.Context;
import android.speech.tts.TextToSpeech;

import java.util.Locale;

/**
 * Created by youjingjing on 2017/11/15.
 */

public class SpeechClient {

    private static final String TAG = "SpeechClient";
    private static volatile SpeechClient singleton;

    private TextToSpeech textToSpeech; // TTS对象


    public SpeechClient(Context context) {
        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {
                    textToSpeech.setLanguage(Locale.US);
                    textToSpeech.setPitch(1.0f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
                    textToSpeech.setSpeechRate(1.0f);
                }
            }
        });
    }

    public static SpeechClient getInstance(Context context) {
        if (singleton == null) {
            synchronized (SpeechClient.class) {
                if (singleton == null) {
                    singleton = new SpeechClient(context.getApplicationContext());
                }
            }
        }
        return singleton;
    }


    public void speakText(String text) {
       textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void stop(){
        textToSpeech.stop();
    }
    public void release(){
        textToSpeech.shutdown();
    }
}
